import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { DicEditComponent } from '../dic-detail/dic-edit.component'
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'dic-list',
  templateUrl: './dic-list.component.html',
  styles: []
})
export class DicListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  editFlag:boolean = this.aclService.canAbility("warning-dic/update");//检查用户按钮权限
  addFlag:boolean =  this.aclService.canAbility("warning-dic/add");
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'eventCode',
        title: '事件码',
        filter: {
          placeholder: '请输入事件码',
          type: 'string'
        }
      },
      {
        index: 'deviceTypeName',
        title: '设备类型'
      },
      {
        index: 'eventName',
        title: '事件'
      },
      {
        index: 'eventType',
        title: '事件类型'
      },
      {
        index: 'status',
        title: '状态',
        type: ColumnType.tag,
        tag:{
          true:{text:'正常',color:'green'},
          false:{text:'异常',color:'red'}
        }
      }
    ],
    showCheckBox: false,
    reqUrl:'/core/event/dictionary/page',
    editBtn: [
      { title: '修改',iif:()=> this.editFlag }
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
  ngOnInit() {

  }


  // 编辑功能开始
  addDic(type: 0 | 1 = 0, _param?: object): void {
    console.log(type);
    const title = type === 0 ? '新增字典' : '修改字典';
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: DicEditComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }

  operate(e: any): void {
    
    if(e.title === '修改'){
      this.addDic(1, e.data);
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  
}
